﻿
Imports AutoMapper
Imports FTTK.Entities
Imports System.Net
Imports System.Globalization
Imports FTTK.Json

Namespace FTTK.Services.Implementations.Rest
    Friend NotInheritable Class PartnerServiceMapping
        Private Sub New()

        End Sub

        ''' <summary>
        ''' Defines the mapping for Automapper.
        ''' </summary>
        ''' <remarks></remarks>
        Friend Shared Sub DefineMapping()
            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Category))() _
                .ConvertUsing(Function(a)
                                  Return From c In a.Values.OfType(Of JsonObject)()
                                         Select Mapper.Map(Of JsonObject, Category)(c)
                              End Function)

            Mapper.CreateMap(Of JsonObject, Category)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name"))) _
                .ForMember(Function(a) a.NumberOfSubCategory, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_sub_categories")))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Tag))() _
                .ConvertUsing(Function(a)
                                  Return From c In a.Values.OfType(Of JsonObject)()
                                         Select Mapper.Map(Of JsonObject, Tag)(c)
                              End Function)

            Mapper.CreateMap(Of JsonObject, Tag)() _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name"))) _
                .ForMember(Function(a) a.Popularity, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("popularity")))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Gallery))() _
                .ConvertUsing(Function(a)
                                  Return From c In a.Values.OfType(Of JsonObject)()
                                         Select Mapper.Map(Of JsonObject, Gallery)(c)
                              End Function)

            Mapper.CreateMap(Of JsonObject, Gallery)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("id"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name"))) _
                .ForMember(Function(a) a.NumberOfImages, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media"))) _
                .ForMember(Function(a) a.Thumbnail, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsOneOfKey({"thumbnail_height", "thumbnail_html_tag", "thumbnail_url", "thumbnail_width"}) Then
                                                                                  Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_height"),
                                                                                                         .HtmlTag = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_html_tag")),
                                                                                                         .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_url")),
                                                                                                         .Width = a.GetValue(Of Integer)("thumbnail_width")}
                                                                              Else
                                                                                  Return Nothing
                                                                              End If
                                                                          End Function)) _
                .ForMember(Function(a) a.Type, Sub(x) x.ResolveUsing(Function(a)
                                                                         If a.ContainsKey("state") Then
                                                                             Select Case a.GetValue(Of Integer)("state")
                                                                                 Case 0
                                                                                     Return GalleryType.Lightbox
                                                                                 Case 1
                                                                                     Return GalleryType.Folder
                                                                             End Select
                                                                         End If
                                                                         Return GalleryType.Unknown
                                                                     End Function))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Color))() _
                 .ConvertUsing(Function(a)
                                   Dim jsonObj = a.GetValue(Of JsonObject)("colors")
                                   If jsonObj IsNot Nothing Then
                                       Dim query = From c In jsonObj.Values.OfType(Of JsonObject)()
                                                    Select Mapper.Map(Of JsonObject, Color)(c)
                                       Return query
                                   Else
                                       Return Nothing
                                   End If
                               End Function)

            Mapper.CreateMap(Of JsonObject, Color)() _
               .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
               .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name")))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Country))() _
               .ConvertUsing(Function(a)
                                 Return From c In a.Values.OfType(Of JsonObject)()
                                        Select Mapper.Map(Of JsonObject, Country)(c)
                             End Function)

            Mapper.CreateMap(Of JsonObject, Country)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("id"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name")))

            Mapper.CreateMap(Of IEnumerable(Of JsonObject), IEnumerable(Of Media))() _
               .ConvertUsing(Function(a)
                                 Return From c In a
                                        Select Mapper.Map(Of JsonObject, Media)(c)
                             End Function)

            Mapper.CreateMap(Of JsonObject, Media)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("title"))) _
                .ForMember(Function(a) a.Creator, Sub(x) x.ResolveUsing(Function(a)
                                                                            Return New User With {.Id = a.GetValue(Of Integer)("creator_id"),
                                                                                                  .Name = a.GetValue(Of String)("creator_name")}
                                                                        End Function)) _
                .ForMember(Function(a) a.Licenses, Sub(x) x.ResolveUsing(Function(a)
                                                                             Dim licenses = a.GetValue(Of IEnumerable(Of JsonObject))("licenses")
                                                                             If licenses IsNot Nothing Then
                                                                                 Dim list = From c In licenses
                                                                                            Select Mapper.Map(Of JsonObject, License)(c)
                                                                                 If a.ContainsKey("licenses_details") Then
                                                                                     Dim details = a.GetValue(Of JsonObject)("licenses_details")
                                                                                     For Each lic In list
                                                                                         Dim detail = details.GetValue(Of JsonObject)(lic.Name)
                                                                                         If detail IsNot Nothing Then
                                                                                             lic.Detail = Mapper.Map(Of JsonObject, LicenseDetail)(detail)
                                                                                         End If
                                                                                     Next
                                                                                 End If
                                                                                 Return list
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function)) _
                .ForMember(Function(a) a.MediaTypeId, Sub(x) x.ResolveUsing(Function(a)
                                                                                If a.ContainsKey("media_type_id") Then
                                                                                    Return a.GetValue(Of Integer)("media_type_id")
                                                                                Else
                                                                                    Return Nothing
                                                                                End If
                                                                            End Function)) _
                .ForMember(Function(a) a.Thumbnail, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsOneOfKey({"thumbnail_url", "thumbnail_height", "thumbnail_html_tag", "thumbnail_width"}) Then
                                                                                  Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_height", -1),
                                                                                                         .HtmlTag = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_html_tag")),
                                                                                                         .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_url")),
                                                                                                         .Width = a.GetValue(Of Integer)("thumbnail_width", -1)}
                                                                              Else
                                                                                  Return Nothing
                                                                              End If
                                                                          End Function)) _
                .ForMember(Function(a) a.Thumbnail110, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsOneOfKey({"thumbnail_110_url", "thumbnail_110_height", "thumbnail_110_width"}) Then
                                                                                     Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_110_height", -1),
                                                                                                             .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_110_url")),
                                                                                                             .Width = a.GetValue(Of Integer)("thumbnail_110_width", -1)}
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.Thumbnail30, Sub(x) x.ResolveUsing(Function(a)
                                                                                If a.ContainsOneOfKey({"thumbnail_30_url", "thumbnail_30_height", "thumbnail_30_width"}) Then
                                                                                    Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_30_height", -1),
                                                                                                            .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_30_url")),
                                                                                                            .Width = a.GetValue(Of Integer)("thumbnail_30_width", -1)}
                                                                                Else
                                                                                    Return Nothing
                                                                                End If
                                                                            End Function)) _
                .ForMember(Function(a) a.Thumbnail400, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsOneOfKey({"thumbnail_400_url", "thumbnail_400_height", "thumbnail_400_width"}) Then
                                                                                     Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_400_height", -1),
                                                                                                             .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_400_url")),
                                                                                                             .Width = a.GetValue(Of Integer)("thumbnail_400_width", -1)}
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.AffiliationLink, Sub(x) x.MapFrom(Function(a) FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("affiliation_link")))) _
                .ForMember(Function(a) a.Views, Sub(x) x.ResolveUsing(Function(a)
                                                                          If a.ContainsKey("nb_views") Then
                                                                              Return a.GetValue(Of Integer)("nb_views")
                                                                          Else
                                                                              Return Nothing
                                                                          End If
                                                                      End Function)) _
                .ForMember(Function(a) a.Downloads, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsKey("nb_downloads") Then
                                                                                  Return a.GetValue(Of Integer)("nb_downloads")
                                                                              Else
                                                                                  Return Nothing
                                                                              End If
                                                                          End Function)) _
                .ForMember(Function(a) a.CreationDate, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsKey("creation_date") Then
                                                                                     Dim creationdate = a.GetValue(Of String)("creation_date")
                                                                                     Dim dateSplit = creationdate.Split("."c)
                                                                                     Return DateTime.ParseExact(dateSplit(0), "yyyy-M-dd HH:mm:ss", CultureInfo.InvariantCulture)
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.ConceptualCategory, Sub(x) x.MapFrom(Function(a) Mapper.Map(Of JsonObject, Category)(a.GetValue(Of JsonObject)("cat2")))) _
                .ForMember(Function(a) a.RepresentativeCategory, Sub(x) x.MapFrom(Function(a) Mapper.Map(Of JsonObject, Category)(a.GetValue(Of JsonObject)("cat1")))) _
                .ForMember(Function(a) a.ConceptualSubCategories, Sub(x) x.ResolveUsing(Function(a)
                                                                                            If a.GetValue(Of IEnumerable(Of JsonObject))("cat2_hierarchy") IsNot Nothing Then
                                                                                                Return From c In a.GetValue(Of IEnumerable(Of JsonObject))("cat2_hierarchy")
                                                                                                       Select Mapper.Map(Of JsonObject, Category)(c)
                                                                                            Else
                                                                                                Return Nothing
                                                                                            End If
                                                                                        End Function)) _
                .ForMember(Function(a) a.RepresentativeSubCategories, Sub(x) x.ResolveUsing(Function(a)
                                                                                                If a.GetValue(Of IEnumerable(Of JsonObject))("cat1_hierarchy") IsNot Nothing Then
                                                                                                    Return From c In a.GetValue(Of IEnumerable(Of JsonObject))("cat1_hierarchy")
                                                                                                            Select Mapper.Map(Of JsonObject, Category)(c)
                                                                                                Else
                                                                                                    Return Nothing
                                                                                                End If
                                                                                            End Function)) _
                .ForMember(Function(a) a.Country, Sub(x) x.ResolveUsing(Function(a)
                                                                            If a.ContainsOneOfKey({"country_id", "country_name"}) Then
                                                                                Dim id = a.GetValue(Of Integer)("country_id", -1)
                                                                                Dim name = a.GetValue(Of String)("country_name")
                                                                                Return New Country() With {.Id = id, .Name = name}
                                                                            Else
                                                                                Return Nothing
                                                                            End If
                                                                        End Function)) _
                .ForMember(Function(a) a.Width, Sub(x) x.ResolveUsing(Function(a)
                                                                          Dim width = a.GetValue(Of Integer)("width", -1)
                                                                          If width > 0 Then
                                                                              Return width
                                                                          Else
                                                                              Return Nothing
                                                                          End If
                                                                      End Function)) _
                .ForMember(Function(a) a.Height, Sub(x) x.ResolveUsing(Function(a)
                                                                           Dim height = a.GetValue(Of Integer)("height", -1)
                                                                           If height > 0 Then
                                                                               Return height
                                                                           Else
                                                                               Return Nothing
                                                                           End If
                                                                       End Function)) _
                .ForMember(Function(a) a.AvailableForSubscription, Sub(x) x.ResolveUsing(Function(a)
                                                                                             Dim available = a.GetValue(Of Integer)("available_for_subscription", -1)
                                                                                             If available <> -1 Then
                                                                                                 Return If(available = 0, False, True)
                                                                                             Else
                                                                                                 Return Nothing
                                                                                             End If
                                                                                         End Function)) _
                .ForMember(Function(a) a.HasReleases, Sub(x) x.ResolveUsing(Function(a)
                                                                                Dim hasRel = a.GetValue(Of Integer)("has_releases", -1)
                                                                                If hasRel <> -1 Then
                                                                                    Return If(hasRel = 0, False, True)
                                                                                Else
                                                                                    Return Nothing
                                                                                End If
                                                                            End Function)) _
                .ForMember(Function(a) a.Keywords, Sub(x) x.ResolveUsing(Function(a)
                                                                             If a.ContainsKey("keywords") Then
                                                                                 Dim obj = a("keywords")
                                                                                 If TypeOf obj Is String Then
                                                                                     Return CType(obj, String).Split(","c)
                                                                                 ElseIf TypeOf obj Is IEnumerable(Of JsonObject) Then
                                                                                     Return From v In CType(obj, IEnumerable(Of JsonObject))
                                                                                            Select v.GetValue(Of String)("name")
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function))



            Mapper.CreateMap(Of JsonObject, IEnumerable(Of License))() _
                .ConvertUsing(Function(a)
                                  Return From c In a.Values.OfType(Of JsonObject)()
                                         Select Mapper.Map(Of JsonObject, License)(c)
                              End Function)

            Mapper.CreateMap(Of JsonObject, License)() _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name"))) _
                .ForMember(Function(a) a.Price, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("price")))

            Mapper.CreateMap(Of JsonObject, LicenseDetail)() _
                .ForMember(Function(a) a.DisplayAbbreviation, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("dimensions"))) _
                .ForMember(Function(a) a.DisplayAbbreviationLocal, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("dimensions_local"))) _
                .ForMember(Function(a) a.Dpi, Sub(x) x.ResolveUsing(Function(a)
                                                                        If a.ContainsKey("dpi") Then
                                                                            Return a.GetValue(Of Integer)("dpi")
                                                                        Else
                                                                            Return Nothing
                                                                        End If
                                                                    End Function)) _
                .ForMember(Function(a) a.Height, Sub(x) x.ResolveUsing(Function(a)
                                                                           Dim str = a.GetValue(Of String)("height")
                                                                           Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                       End Function)) _
                .ForMember(Function(a) a.Phrase, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("phrase"))) _
                .ForMember(Function(a) a.Ratio, Sub(x) x.ResolveUsing(Function(a)
                                                                          Dim str = a.GetValue(Of String)("ratio")
                                                                          Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                      End Function)) _
                .ForMember(Function(a) a.Width, Sub(x) x.ResolveUsing(Function(a)
                                                                          Dim str = a.GetValue(Of String)("width")
                                                                          Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                      End Function))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Image))() _
                .ConvertUsing(Function(a)
                                  Return From c In a.Values.OfType(Of JsonObject)()
                                         Select Mapper.Map(Of JsonObject, Image)(c)
                              End Function)

            Mapper.CreateMap(Of JsonObject, Image)() _
                .ForMember(Function(a) a.Url, Sub(x) x.MapFrom(Function(a) FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("url")))) _
                .ForMember(Function(a) a.Height, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("height"))) _
                .ForMember(Function(a) a.Width, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("width"))) _
                .ForMember(Function(a) a.HtmlTag, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("htmltag")))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of HomeImage))() _
               .ConvertUsing(Function(a)
                                 Return From c In a.Values.OfType(Of JsonObject)()
                                        Select Mapper.Map(Of JsonObject, HomeImage)(c)
                             End Function)

            Mapper.CreateMap(Of JsonObject, HomeImage)() _
                .ForMember(Function(a) a.Src, Sub(x) x.MapFrom(Function(a) FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("src")))) _
                .ForMember(Function(a) a.Url, Sub(x) x.MapFrom(Function(a) FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("url")))) _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id")))

            Mapper.CreateMap(Of JsonObject, UserData)() _
                .ForMember(Function(a) a.CreditValue, Sub(x) x.ResolveUsing(Function(a)
                                                                                Dim str = a.GetValue(Of String)("credit_value")
                                                                                Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                            End Function)) _
                .ForMember(Function(a) a.Currency, Sub(x) x.ResolveUsing(Function(a)
                                                                             If a.ContainsOneOfKey({"currency_name", "currency_symbol"}) Then
                                                                                 Dim symbol = a.GetValue(Of String)("currency_symbol")
                                                                                 Dim name = a.GetValue(Of String)("currency_name")
                                                                                 Return New Currency() With {.Simbol = symbol, .Name = name}
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function)) _
                .ForMember(Function(a) a.HasSubscription, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Boolean)("has_subscription"))) _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
                .ForMember(Function(a) a.Language, Sub(x) x.ResolveUsing(Function(a)
                                                                             If a.ContainsOneOfKey({"language_id", "language_name"}) Then
                                                                                 Dim id = a.GetValue(Of Integer)("language_id")
                                                                                 Dim name = a.GetValue(Of String)("language_name")
                                                                                 Return New Language() With {.Id = id, .Name = name}
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function)) _
                .ForMember(Function(a) a.LastName, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("lastname"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("firstname"))) _
                .ForMember(Function(a) a.NumberOfCredits, Sub(x) x.ResolveUsing(Function(a)
                                                                                    Dim str = a.GetValue(Of String)("nb_credits")
                                                                                    Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                                End Function)) _
                .ForMember(Function(a) a.NumberOfCreditsLocalized, Sub(x) x.ResolveUsing(Function(a)
                                                                                             Dim str = a.GetValue(Of String)("nb_credits_localized")
                                                                                             str = str.Replace(",", ".")
                                                                                             Return Double.Parse(str, CultureInfo.InvariantCulture)
                                                                                         End Function)) _
                .ForMember(Function(a) a.PreferredVideoFormat, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("preferred_video_format")))

            Mapper.CreateMap(Of JsonObject, UserStats)() _
                .ForMember(Function(a) a.MediaAccepted, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media_accepted"))) _
                .ForMember(Function(a) a.MediaPurchased, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media_purchased"))) _
                .ForMember(Function(a) a.MediaSold, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media_sold"))) _
                .ForMember(Function(a) a.MediaUploaded, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media_uploaded"))) _
                .ForMember(Function(a) a.RankingAbsolute, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("ranking_absolute"))) _
                .ForMember(Function(a) a.RankingRelative, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("ranking_relative")))

            Mapper.CreateMap(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStats)() _
                .ForMember(Function(a) a.MemberBoughtPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                       Select Case a.Item2
                                                                                           Case UserStatsType.All
                                                                                               Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                   (New Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                    (CType(a.Item1.Item("member_bought_photos"), JsonObject), a.Item2, a.Item3))
                                                                                           Case UserStatsType.MemberBoughtPhotos
                                                                                               Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                           Case Else
                                                                                               Return Nothing
                                                                                       End Select
                                                                                   End Function)) _
                .ForMember(Function(a) a.MemberDownloadedPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                           Select Case a.Item2
                                                                                               Case UserStatsType.All
                                                                                                   Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                       (New Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                       (CType(a.Item1.Item("member_downloaded_photos"), JsonObject), a.Item2, a.Item3))
                                                                                               Case UserStatsType.MemberDownloadedPhotos
                                                                                                   Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                               Case Else
                                                                                                   Return Nothing
                                                                                           End Select
                                                                                       End Function)) _
                .ForMember(Function(a) a.MemberEarnedCredits, Sub(x) x.ResolveUsing(Function(a)
                                                                                        Select Case a.Item2
                                                                                            Case UserStatsType.All
                                                                                                Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                    (New Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                    (CType(a.Item1.Item("member_earned_credits"), JsonObject), a.Item2, a.Item3))
                                                                                            Case UserStatsType.MemberEarnedCredits
                                                                                                Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                            Case Else
                                                                                                Return Nothing
                                                                                        End Select
                                                                                    End Function)) _
                .ForMember(Function(a) a.MemberViewedPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                       Select Case a.Item2
                                                                                           Case UserStatsType.All
                                                                                               Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                   (New Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                   (CType(a.Item1.Item("member_viewed_photos"), JsonObject), a.Item2, a.Item3))
                                                                                           Case UserStatsType.MemberViewedPhotos
                                                                                               Return Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                           Case Else
                                                                                               Return Nothing
                                                                                       End Select
                                                                                   End Function))


            Mapper.CreateMap(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)() _
                .ForMember(Function(a) a.ToBeGenerated, Sub(x) x.MapFrom(Function(a) a.Item1.GetValue(Of Boolean)("to_be_generated", False))) _
                .ForMember(Function(a) a.Details, Sub(x) x.MapFrom(Function(a) Mapper.Map(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))(a)))

            Mapper.CreateMap(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))() _
                .ConvertUsing(Of JsonObjectToEnumerableDetailsConverter)()

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of GalleryTheme))() _
               .ConvertUsing(Function(a)
                                 Return From c In a.Values.OfType(Of JsonObject)()
                                        Select Mapper.Map(Of JsonObject, GalleryTheme)(c)
                             End Function)

            Mapper.CreateMap(Of JsonObject, GalleryTheme)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("theme_id"))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name"))) _
                .ForMember(Function(a) a.Thumbnail, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsOneOfKey({"thumbnail_height", "thumbnail_html_tag", "thumbnail_url", "thumbnail_width"}) Then
                                                                                  Return New Image With {.Height = a.GetValue(Of Integer)("thumbnail_height"),
                                                                                                         .HtmlTag = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_html_tag")),
                                                                                                         .Url = FTTK.Utilities.WebHelper.ClearHtml(a.GetValue(Of String)("thumbnail_url")),
                                                                                                         .Width = a.GetValue(Of Integer)("thumbnail_width")}
                                                                              Else
                                                                                  Return Nothing
                                                                              End If
                                                                          End Function)) _
                .ForMember(Function(a) a.Galleries, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsKey("galleries") Then
                                                                                  Dim values = CType(a("galleries"), IEnumerable(Of JsonObject))
                                                                                  Return From v In values.OfType(Of JsonObject)()
                                                                                            Select Mapper.Map(Of JsonObject, Gallery)(v)
                                                                              End If
                                                                              Return Nothing
                                                                          End Function))


        End Sub

        Private Class JsonObjectToEnumerableDetailsConverter
            Implements ITypeConverter(Of Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))

            Public Function Convert(context As AutoMapper.ResolutionContext) As System.Collections.Generic.IEnumerable(Of Entities.UserAdvancedStatsDetail) Implements AutoMapper.ITypeConverter(Of System.Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange), System.Collections.Generic.IEnumerable(Of Entities.UserAdvancedStatsDetail)).Convert
                Dim sourceTuple = CType(context.SourceValue, Tuple(Of JsonObject, Entities.UserStatsType, Entities.UserStatsTimeRange))
                Dim list = New List(Of Entities.UserAdvancedStatsDetail)()
                For Each key In sourceTuple.Item1.Keys.OfType(Of String)()
                    Dim detail As UserAdvancedStatsDetail = Nothing
                    Select Case sourceTuple.Item3
                        Case UserStatsTimeRange.Day
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 2 Then
                                Try
                                    detail = New UserAdvancedStatsDayDetail()
                                    CType(detail, UserAdvancedStatsDayDetail).Day = CInt(datepart(2))
                                    CType(detail, UserAdvancedStatsDayDetail).Month = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Month
                            Dim datepart = key.Split(" "c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsMonthDetail()
                                    CType(detail, UserAdvancedStatsMonthDetail).Month = datepart(0)
                                    detail.Year = CInt(datepart(1))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Quarter
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsQuarterDetail()
                                    CType(detail, UserAdvancedStatsQuarterDetail).Quarter = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Week
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsWeekDetail()
                                    CType(detail, UserAdvancedStatsWeekDetail).Week = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Year
                            Try
                                detail = New UserAdvancedStatsYearDetail()
                                detail.Year = CInt(key)
                            Catch ex As Exception
                                detail = Nothing
                            End Try
                    End Select
                    If detail IsNot Nothing Then
                        detail.Value = Double.Parse(sourceTuple.Item1.GetValue(Of String)(key, "0"), New CultureInfo("en"))
                        list.Add(detail)
                    End If
                Next
                Return list
            End Function
        End Class
    End Class


End Namespace
